package when3meet.view;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JRadioButton;

import java.awt.Font;

import javax.swing.ButtonGroup;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import javax.swing.JButton;

import when3meet.controller.CloseEventRadioBtnController;
import when3meet.controller.CreateEventController;

public class CreateEventFrame extends JFrame {

	public static CreateEventFrame ceFrame = null;
	JTextField eventNameField;
	JTextField maxEdgeField;
	JTextField maxChoiceField;
	JRadioButton openEventRBtn;
	JRadioButton closeEventRBtn;
	JRadioButton asynchronousRBtn;
	JRadioButton roundrobinRBtn;
	GroupLayout gl_contentPane;
	JButton submitBtn;

	public static CreateEventFrame getInstance() {

		if (ceFrame == null) {
			ceFrame = new CreateEventFrame();
		}

		return ceFrame;
	}

	/**
	 * Create the frame.
	 */
	private CreateEventFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 409, 306);
		JPanel contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		setLocationRelativeTo(null);

		JLabel lblCreateEvent = new JLabel("Create Event");
		lblCreateEvent.setFont(new Font("Tahoma", Font.BOLD, 17));

		JLabel lblEventName = new JLabel("Event Name");

		eventNameField = new JTextField();
		eventNameField.setColumns(10);

		JLabel lblMaxiumEdge = new JLabel("Maxium Edge");

		maxEdgeField = new JTextField();
		maxEdgeField.setColumns(10);

		// add round robin, asynchronous to button group

		ButtonGroup bg = new ButtonGroup();

		roundrobinRBtn = new JRadioButton("Round-robin");
		roundrobinRBtn.setSelected(true);

		asynchronousRBtn = new JRadioButton("Asynchronous");

		bg.add(roundrobinRBtn);
		bg.add(asynchronousRBtn);

		// add open, close radio button to button group

		ButtonGroup bg1 = new ButtonGroup();

		openEventRBtn = new JRadioButton("Open Event");
		openEventRBtn.setSelected(true);

		closeEventRBtn = new JRadioButton("Close Event");

		// bind action when close event radio button is selected
		closeEventRBtn.addActionListener(new CloseEventRadioBtnController(this));

		JLabel lblMaxiumChoice = new JLabel("Maxium Choice");

		maxChoiceField = new JTextField();
		maxChoiceField.setColumns(10);

		// bind action to submit button
		submitBtn = new JButton("Submit");
		submitBtn.addActionListener(new CreateEventController(this));

		gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane
				.setHorizontalGroup(gl_contentPane
						.createParallelGroup(Alignment.TRAILING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addGap(36)
										.addGroup(
												gl_contentPane
														.createParallelGroup(Alignment.LEADING)
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addGroup(
																				gl_contentPane
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(lblEventName)
																						.addComponent(lblMaxiumEdge))
																		.addGap(20))
														.addComponent(roundrobinRBtn, GroupLayout.DEFAULT_SIZE, 109,
																Short.MAX_VALUE).addComponent(openEventRBtn)
														.addComponent(lblMaxiumChoice))
										.addPreferredGap(ComponentPlacement.RELATED)
										.addGroup(
												gl_contentPane
														.createParallelGroup(Alignment.LEADING)
														.addComponent(lblCreateEvent)
														.addGroup(
																Alignment.TRAILING,
																gl_contentPane
																		.createSequentialGroup()
																		.addGroup(
																				gl_contentPane
																						.createParallelGroup(
																								Alignment.TRAILING)
																						.addComponent(closeEventRBtn,
																								Alignment.LEADING)
																						.addComponent(
																								eventNameField,
																								GroupLayout.DEFAULT_SIZE,
																								192, Short.MAX_VALUE)
																						.addComponent(asynchronousRBtn,
																								Alignment.LEADING)
																						.addComponent(
																								maxEdgeField,
																								GroupLayout.DEFAULT_SIZE,
																								192, Short.MAX_VALUE)
																						.addComponent(
																								maxChoiceField,
																								GroupLayout.DEFAULT_SIZE,
																								192, Short.MAX_VALUE))
																		.addGap(15).addComponent(submitBtn)))
										.addContainerGap()));
		gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(
				gl_contentPane
						.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblCreateEvent)
						.addGap(27)
						.addGroup(
								gl_contentPane
										.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblEventName)
										.addComponent(eventNameField, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addGroup(
								gl_contentPane
										.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblMaxiumEdge)
										.addComponent(maxEdgeField, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addGroup(
								gl_contentPane
										.createParallelGroup(Alignment.BASELINE)
										.addComponent(maxChoiceField, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblMaxiumChoice))
						.addGap(21)
						.addGroup(
								gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(asynchronousRBtn)
										.addComponent(roundrobinRBtn))
						.addGap(18)
						.addGroup(
								gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(closeEventRBtn)
										.addComponent(openEventRBtn).addComponent(submitBtn)).addContainerGap()));

		bg1.add(openEventRBtn);
		bg1.add(closeEventRBtn);
		contentPane.setLayout(gl_contentPane);
	}

	public JTextField getMaxChoiceField() {

		return maxChoiceField;
	}

	public JTextField getEventNameField() {
		return eventNameField;
	}

	public JTextField getMaxEdgeField() {
		return maxEdgeField;
	}

	public JRadioButton getOpenEventRBtn() {
		return openEventRBtn;
	}

	public JRadioButton getCloseEventRBtn() {
		return closeEventRBtn;
	}

	public JRadioButton getAsynchronousRBtn() {
		return asynchronousRBtn;
	}

	public String getEventBehavior() {
		if (this.getAsynchronousRBtn().isSelected())
			return "asynchronous";

		return "roundRobin";
	}

	public String getEventType() {

		if (this.getOpenEventRBtn().isSelected())
			return "open";
		return "closed";
	}

	public JButton getSubmitBtn() {
		return submitBtn;
	}
}
